<?php
if(!function_exists('parse_padding')){
        function parse_padding($source)
        {
            $length  = strlen(strval(count($source['source']) + $source['first']));
            return 40 + ($length - 1) * 8;
        }
}

if(!function_exists('parse_class')){
    function parse_class($name)
    {
        $names = explode('\\', $name);
        return '<abbr title="'.$name.'">'.end($names).'</abbr>';
    }
}

if(!function_exists('parse_file')){
    function parse_file($file, $line)
    {
        return '<a class="toggle" title="'."{$file} line {$line}".'">'.basename($file)." line {$line}".'</a>';
    }
}

if(!function_exists('parse_args')){
    function parse_args($args)
    {
        $result = [];

        foreach ($args as $key => $item) {
            switch (true) {
                case is_object($item):
                    $value = sprintf('<em>object</em>(%s)', parse_class(get_class($item)));
                break;
                case is_array($item):
                    if(count($item) > 3){
                    $value = sprintf('[%s, ...]', parse_args(array_slice($item, 0, 3)));
                    } else {
                    $value = sprintf('[%s]', parse_args($item));
                    }
                break;
                case is_string($item):
                    if(strlen($item) > 20){
                    $value = sprintf(
                    '\'<a class="toggle" title="%s">%s...</a>\'',
                    htmlentities($item),
                    htmlentities(substr($item, 0, 20))
                    );
                    } else {
                    $value = sprintf("'%s'", htmlentities($item));
                    }
                break;
                case is_int($item):
                case is_float($item):
                    $value = $item;
                break;
                case is_null($item):
                    $value = '<em>null</em>';
                break;
                case is_bool($item):
                    $value = '<em>' . ($item ? 'true' : 'false') . '</em>';
                break;
                case is_resource($item):
                    $value = '<em>resource</em>';
                break;
                default:
                    $value = htmlentities(str_replace("\n", '', var_export(strval($item), true)));
                break;
            }

            $result[] = is_int($key) ? $value : "'{$key}' => {$value}";
        }

        return implode(', ', $result);
    }
}


?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>系统发生错误</title>
    <style>
        body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,blockquote,th,td { margin:0; padding:0; box-sizing: border-box; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; -ms-box-sizing: border-box;}
        body { background:#fff; color:#555; font-size:14px; font-family: "Microsoft Yahei"; }
        a { color:#555; text-decoration:none; }
        a:hover { text-decoration:none; }
        img { border:none; }
        .clearfix:after, .clearfix:before {content: ""; display: block; height:0; clear:both; visibility: hidden;}
        .clearfix { *zoom:1; }
        .errPage{width:800px;margin:0 auto;}
        .errPage .header{margin-top:160px;}
        .errPage .header .left{width:290px;height:290px;float:left;}
        .errPage .header .left img{width:100%;height:100%;}
        .errPage .header .right{width:395px;float:right;margin-top:15px;margin-right: 50px;}
        .errPage .header .right .picture{width:228px;height:80px;}
        .errPage .header .right .picture img{width:100%;height:100%;}
        .errPage .header .right .title{font-size:20px;color:#979DAB;margin-top:26px;height:70px;}
        .errPage .header .right .question{margin-top:40px;}
        .errPage .header .right .question .btn{-moz-user-select:none;-webkit-user-select:none;-ms-user-select:none;-khtml-user-select:none;user-select:none;display:inline-block;*display:inline;zoom:0;width:116px;height:28px;border-radius:18px;background-color:#999;font-size:12px;color:#fff;text-align:center;line-height:28px;cursor:pointer;}
        .errPage .header .right .question .btn.on{background-color:#1951FC;}
        .errPage .header .right .question .btn img{width:10px;height:6px;margin-left:10px;}
        .errPage .conter{width:800px;height:424px;padding:25px 29px;background-color:#F7F7F7;border-radius:18px;overflow:auto;font-size:14px;color:#666666;margin-top:55px;line-height:2.4;display:none;}
        .source-code pre {margin: 0;}
        .source-code pre li {height: 18px;line-height: 18px;}
        .source-code pre ol{margin: 0;color: #4288ce;display: inline-block;min-width: 100%;box-sizing: border-box;font-size:14px;font-family: "Century Gothic",Consolas,"Liberation Mono",Courier,Verdana;padding-left: <?php echo (isset($source) && !empty($source)) ? parse_padding($source) : 40;  ?>px;}
        .line-error{background: #f8cbcb;}
        /* SPAN elements with the classes below are added by prettyprint. */
        pre.prettyprint .pln { color: #000 }  /* plain text */
        pre.prettyprint .str { color: #080 }  /* string content */
        pre.prettyprint .kwd { color: #008 }  /* a keyword */
        pre.prettyprint .com { color: #800 }  /* a comment */
        pre.prettyprint .typ { color: #606 }  /* a type name */
        pre.prettyprint .lit { color: #066 }  /* a literal value */
        /* punctuation, lisp open bracket, lisp close bracket */
        pre.prettyprint .pun, pre.prettyprint .opn, pre.prettyprint .clo { color: #660 }
        pre.prettyprint .tag { color: #008 }  /* a markup tag name */
        pre.prettyprint .atn { color: #606 }  /* a markup attribute name */
        pre.prettyprint .atv { color: #080 }  /* a markup attribute value */
        pre.prettyprint .dec, pre.prettyprint .var { color: #606 }  /* a declaration; a variable name */
        pre.prettyprint .fun { color: red }  /* a function name */
        h2 {color: #4288ce;font-weight: 400;padding: 6px 0;margin: 6px 0 0;font-size: 14px;border-bottom: 1px solid #eee;}
        abbr {cursor: help;text-decoration: underline;text-decoration-style: dotted;}
        h1 {margin: 10px 0 0;font-size: 28px;font-weight: 500;line-height: 32px;}
        .source-code::-webkit-scrollbar{width:10px;height:10px;}
        .source-code::-webkit-scrollbar-track{border-radius:2px;}
        .source-code::-webkit-scrollbar-thumb{background: #bfbfbf;border-radius:10px;}
        .source-code::-webkit-scrollbar-thumb:hover{background: #333;}
    </style>
</head>
<body>
<div class="errPage">
    <div class="header">
        <div class="left"><img src=""></div>
        <div class="right">
            <div class="picture"><img src=""></div>
            <div class="title">
                <div class="info">
                    <div>
                        <h2><?php echo nl2br(htmlentities($message)); ?></h2>
                    </div>
                    <div>
                        <h1 style="font-size: 14px;">[<?php echo $code; ?>]&nbsp;<?php echo isset($name) ? sprintf('%s in %s', parse_class($name), parse_file($file, $line)) : $exception->getMessage(); ?></h1>
                    </div>
                </div>
            </div>
            <div class="question">
                <div class="btn">查看错误代码<img src=""></div>
                <div class="btn on"><a style="color: #ffffff" href="javascript:history.back(-1)">返回上一页</a></div>
            </div>
        </div>
    </div>
    <div class="clearfix"></div>
    <div class="conter source-code">
        <?php if(!empty($source)){?>
        <div class="source-code">
            <pre class="prettyprint lang-php"><ol start="<?php echo $source['first']; ?>"><?php foreach ((array) $source['source'] as $key => $value) { ?><li class="line-<?php echo $key + $source['first']; ?>"><code><?php echo htmlentities($value); ?></code></li><?php } ?></ol></pre>
        </div>
        <?php }?>
    </div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>

    var LINE =<?php echo isset($line) ? $line : 0; ?>;
    var down = "";
    var up = "";
    $(document).ready(function(){

        $.getScript = function(src, func){
            var script = document.createElement('script');
            
            script.async  = 'async';
            script.src    = src;
            script.onload = func || function(){};
            
            $('head')[0].appendChild(script);
        }

        var k = true;
        $(".question .btn").on('click',function(){
            if(k){
                $('.conter').show();
                $(this).addClass('on');
                $(this).find('img').attr('src',down);
                k = false;
            }else {
                $('.conter').hide();
                $(this).removeClass('on');
                $(this).find('img').attr('src',up);
                k = true;
            }
       });
        var ol    = $('ol', $('.prettyprint')[0]);
        // 设置出错行
        var err_line = $('.line-' + LINE, ol[0])[0];
        if(err_line) err_line.className = err_line.className + ' line-error';
        $.getScript('//cdn.bootcss.com/prettify/r298/prettify.min.js', function(){
            prettyPrint();

            // 解决Firefox浏览器一个很诡异的问题
            // 当代码高亮后，ol的行号莫名其妙的错位
            // 但是只要刷新li里面的html重新渲染就没有问题了
            if(window.navigator.userAgent.indexOf('Firefox') >= 0){
                ol[0].innerHTML = ol[0].innerHTML;
            }
        });
    });
</script>
</body>
</html>